<template>
  <div class="login-container">
    <!--背景-->
    <div class="noite"></div>
    <!--星星-->
    <div class="constelacao">
      <span class="estrela"
            ref="estrela"
            :style="starStyle[String(getRandomArbitrary(0,4))]"
            :class="getRandomArbitrary(0,5)===2&&getRandomArbitrary(0,5)===3?'twinkle':''"
            v-for="item in stars"
            :key="item"></span>
    </div>
    <!--月亮-->
    <div class="lua">
      <div class="textura"
           @click="createMeteoro"></div>
    </div>
    <!--流星-->
    <div class="chuvaMeteoro">
      <div class="meteoro"
           :class="'meteoroAnimation' + num"
           ref="meteoroRef"></div>
    </div>
    <div class="floresta">
      <img src="../assets/bgTree.png"
           alt="" />
    </div>
    <h1>
      密码默认
      <br>
      宿舍管理员账号：201706401211
      <br>
      学生纪检员：201706401212
      <br>
      学生：201706401215
    </h1>
    <div class="login_box">
      <!--头像区域-->
      <div class="avatar_box">
        <img src="../assets/timg.png"
             alt="图片不见了">
      </div>
      <!--登录表单区域-->
      <el-form label-width="00px"
               class="login_form"
               :model="loginForm"
               ref="loginFromRef"
               :rules="loginFromRules">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input type="password"
                    v-model="loginForm.password"
                    prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item class="btns">
          <el-button type="primary"
                     round
                     @click="login">登录</el-button>
          <el-button type="info"
                     round
                     @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
import CryptoJS from 'crypto-js'
export default {
  name: 'Login',
  props: [''],
  components: {
  },
  data () {
    return {
      // 星星个数
      stars: 250,
      num: 0,
      starStyle: {
        1: {
          width: '1px',
          height: '1px',
          opacity: this.getRandomArbitrary(5, 11) / 10
        },
        2: {
          width: '2px',
          height: '2px',
          opacity: this.getRandomArbitrary(5, 11) / 10
        },
        3: {
          width: '3px',
          height: '3px',
          opacity: this.getRandomArbitrary(5, 11) / 10
        },
        0: {
          width: '4px',
          height: '4px',
          opacity: this.getRandomArbitrary(5, 11) / 10
        }
      },
      // 登录表单的数据绑定对象
      loginForm: {
        username: '201706401203',
        password: 'a123'
      },
      // 表单验证
      loginFromRules: {
        // 账户
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' }
        ],
        // 密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.starsStyle()
    this.timer = setInterval(() => {
      this.createMeteoro()
    }, 1000)
  },
  beforeDestroy () {
    // 页面销毁时删除定时器
    clearInterval(this.timer)
  },
  methods: {
    login () {
      // console.log(CryptoJS.MD5('123456').toString())
      this.$refs.loginFromRef.validate(async valid => {
        if (!valid) return 0
        const data = {
          username: this.loginForm.username,
          password: CryptoJS.MD5(this.password).toString()
          // password: this.loginForm.password
        }
        const { data: res } = await this.$axios.post('users/signin', data)
        // this.$store.dispatch('addTokenToState', res.studentlist[0].token)
        if (res.data.code === 0) {
          this.$message.success('登录成功')
          this.$store.dispatch('login/addTokenToState', res.data.token)
          this.$store.dispatch('login/addStudentNumber', res.data.user)
          this.$store.dispatch('authority/getRole', res.data.user.role)
          this.$router.push('/welcome')
          console.log(res)
        } else {
          this.$message.error('登录失败，请确认密码')
        }
      })
    },
    register () {
      this.$router.push('/register')
    },
    // 返回随机数
    getRandomArbitrary (min, max, decimal) {
      return Math.floor(Math.random() * (max - min)) + min
    },
    // 给星星添加样式
    starsStyle () {
      const estrela = this.$refs.estrela
      // console.log(estrela[0].style)
      estrela.forEach(valid => {
        valid.style.left = this.getRandomArbitrary(0, window.innerWidth) + 'px'
        valid.style.top = this.getRandomArbitrary(0, window.innerHeight * 2) + 'px'
        valid.style.animationDuration = '.' + this.getRandomArbitrary(0, 9) + 's'
      })
    },
    // 流星
    createMeteoro () {
      this.$refs.meteoroRef.className = this.$refs.meteoroRef.className.slice(0, -1) + this.getRandomArbitrary(0, 4)
    }
  }
}
</script>
<style lang="less" scoped>
@import '../assets/css/starrySky.less';
h1 {
  position: absolute;
  color: #fff;
  z-index: 999;
}
.login_box {
  // 登录界面居中
  width: 450px;
  height: 300px;
  background: -webkit-linear-gradient(
    top,
    rgb(0, 0, 0) 50%,
    rgb(25, 19, 39) 80%,
    rgb(43, 32, 72)
  );
  // 设置圆角
  border-radius: 10px;
  position: absolute;
  // 距离左上各距离50%
  left: 50%;
  top: 50%;
  // 横轴纵轴偏移50%
  transform: translate(-50%, -50%);
  opacity: 0.9;
  border: 1px solid rgb(43, 32, 72);
  .avatar_box {
    height: 130px;
    width: 130px;
    background: -webkit-linear-gradient(
      top,
      rgb(0, 0, 0) 50%,
      rgb(25, 19, 39) 80%,
      rgb(43, 32, 72)
    );
    // 添加边框
    border: 1px solid #eee;
    // 变宽变成圆角
    border-radius: 50%;
    padding: 10px;
    // 给外边款家阴影
    box-shadow: 0 0 10px #ddd;
    // 将这个盒子放置于主盒子正上方
    position: absolute;
    left: 50%;
    // 再移动图片自身的﹣50%,横向和纵向（左边和上边）是根据avatar_box来移动，也就是65px
    transform: translate(-50%, -50%);
    background-color: #fff;
  }
  // 让图片的宽和高等于副盒子的宽高
  img {
    width: 100%;
    height: 100%;
    //图片也变为圆角
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  //不让账号密码栏超出login-contain外面
  box-sizing: border-box;
}

.btns {
  // 让按钮居右对其
  display: flex;
  justify-content: flex-end;
}
</style>
